package top.codestudy.viewtest;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.request.RequestOptions;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;

import javax.security.auth.login.LoginException;

import top.codestudy.viewtest.service.MyService;
import top.codestudy.viewtest.widget.MyTextView;
import top.codestudy.viewtest.widget.NumberView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private String TAG = getClass().getSimpleName();
    private ImageView imageView;
    private ImageView ivImgLarge;
    private ImageView ivImgMedium;
    private ImageView ivImgSmall;
    private ImageView ivImgTiny;

    private SubsamplingScaleImageView imageView2;


//"large": "https://images.pexels.com/photos/2014422/pexels-photo-2014422.jpeg?auto=compress&cs=tinysrgb&h=650&w=940",
//        "medium": "https://images.pexels.com/photos/2014422/pexels-photo-2014422.jpeg?auto=compress&cs=tinysrgb&h=350",
//        "small": "https://images.pexels.com/photos/2014422/pexels-photo-2014422.jpeg?auto=compress&cs=tinysrgb&h=130",
//        "portrait": "https://images.pexels.com/photos/2014422/pexels-photo-2014422.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800",
//        "landscape": "https://images.pexels.com/photos/2014422/pexels-photo-2014422.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=627&w=1200",
//        "tiny": "https://images.pexels.com/photos/2014422/pexels-photo-2014422.jpeg?auto=compress&cs=tinysrgb&dpr=1&fit=crop&h=200&w=280"
//}


    private String url ="https://images.pexels.com/photos/2014422/pexels-photo-2014422.jpeg";
    private String url_Large ="https://images.pexels.com/photos/2014422/pexels-photo-2014422.jpeg?auto=compress&cs=tinysrgb&h=650&w=940";
    private String url_Medium ="https://images.pexels.com/photos/2014422/pexels-photo-2014422.jpeg?auto=compress&cs=tinysrgb&h=350";
    private String url_Small ="https://images.pexels.com/photos/2014422/pexels-photo-2014422.jpeg?auto=compress&cs=tinysrgb&h=130";
    private String url_Tiny="https://images.pexels.com/photos/2014422/pexels-photo-2014422.jpeg?auto=compress&cs=tinysrgb&dpr=1&fit=crop&h=200&w=280";

    GlideUrl glideUrl = new GlideUrl(url, new LazyHeaders.Builder()
            .addHeader("Authorization", "563492ad6f917000010000018808689679754a6399860c6e1457a91c")
            .build());
    GlideUrl glideUrl1 = new GlideUrl(url_Large, new LazyHeaders.Builder()
            .addHeader("Authorization", "563492ad6f917000010000018808689679754a6399860c6e1457a91c")
            .build());
    GlideUrl glideUrl2 = new GlideUrl(url_Medium, new LazyHeaders.Builder()
            .addHeader("Authorization", "563492ad6f917000010000018808689679754a6399860c6e1457a91c")
            .build());
    GlideUrl glideUrl3 = new GlideUrl(url_Small, new LazyHeaders.Builder()
            .addHeader("Authorization", "563492ad6f917000010000018808689679754a6399860c6e1457a91c")
            .build());
    GlideUrl glideUrl4 = new GlideUrl(url_Tiny, new LazyHeaders.Builder()
            .addHeader("Authorization", "563492ad6f917000010000018808689679754a6399860c6e1457a91c")
            .build());
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate: ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.iv_show_img);
        ivImgLarge = (ImageView) findViewById(R.id.iv_img_large);
        ivImgMedium = (ImageView) findViewById(R.id.iv_img_medium);
        ivImgSmall = (ImageView) findViewById(R.id.iv_img_small);
        ivImgTiny = (ImageView) findViewById(R.id.iv_img_tiny);
        imageView2 = (SubsamplingScaleImageView) findViewById(R.id.imageView);
        imageView.setOnClickListener(this);
        imageView2.setOnClickListener(this);
        imageView2.setMaxScale(2F);
        Glide.with(this)
                .load(glideUrl)
                .error(R.mipmap.ic_launcher)
                .into(imageView);

        Glide.with(this)
                .load(glideUrl1)
                .error(R.mipmap.ic_launcher)
                .into(ivImgLarge);
        Glide.with(this)
                .load(glideUrl2)
                .error(R.mipmap.ic_launcher)
                .into(ivImgMedium);
        Glide.with(this)
                .load(glideUrl3)
                .error(R.mipmap.ic_launcher)
                .into(ivImgSmall);
        Glide.with(this)
                .load(glideUrl4)
                .error(R.mipmap.ic_launcher)
                .into(ivImgTiny);


    }

    public void test(){
        RequestOptions sharedOptions =
                new RequestOptions()
                        .fitCenter();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_show_img:
                Log.i(TAG, "onClick: iv_show_img");
                imageView2.setImage(ImageSource.bitmap(drawableToBitmap(imageView.getDrawable())));
                imageView2.setVisibility(View.VISIBLE);
                break;
            case R.id.imageView:
                Log.i(TAG, "onClick: imageView");
                imageView2.setVisibility(View.GONE);
                break;
        }
    }

    public static final Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }

}

